﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MealBooking.Core;
using MealBooking.Core.Models.Meals;
using ThisIsFirstTest.Models.MealModel;
using ThisIsFirstTest.Filter;
using MealBooking.Core.Models.Accounts;
using MealBooking.Core.Models.Dishes;
using MealBooking.Core.Common;

namespace ThisIsFirstTest.Areas.Manager.Controllers
{
    [AccountAuthorize(AccountRoles = Role.Admin | Role.Manager)]
    public class MealManagerController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        #region Meal Category
        public ActionResult AllMealCategories()
        {
            var allMealCategories = CoreService.MealService.AllMealCategories();
            return View(allMealCategories);
        }
        public ActionResult MealCategoryDetail(string id, string headerMessage = null)
        {
            if (headerMessage != null)
            {
                ViewBag.HeaderMessage = headerMessage;
            }
            else
            {
                ViewBag.HeaderMessage = "Meal Category Detail";
            }
            var mealCategory = CoreService.MealService.MealCategoryDetail(id);
            return View(mealCategory);
        }
        public ActionResult AddNewMealCategory()
        {
            return View();
        }
        [HttpPost]
        public ActionResult AddNewMealCategory(MealCategoryModel newMealCategory)
        {
            if (!ModelState.IsValid)
            {
                return View();
            }
            var sameNameCategory = CoreService.MealService.IsMealCategoryNameInUse(newMealCategory.Name);
            if (sameNameCategory != null)
            {
                ViewBag.SameNameMealCategory = sameNameCategory;
                return View(newMealCategory);
            }
            var mealCategory = Convert(newMealCategory);
            mealCategory = CoreService.MealService.AddNewMealCategory(mealCategory);
            return RedirectToAction("MealCategoryDetail", new { id = mealCategory.Id });
        }
        public ActionResult EditMealCategory(string id)
        {
            var mealCategory = CoreService.MealService.MealCategoryDetail(id);
            return View(mealCategory);
        }
        [HttpPost]
        public ActionResult EditMealCategory(MealCategoryModel newMealCategory)
        {
            if (!ModelState.IsValid)
            {
                return View(newMealCategory);
            }
            var mealCategory = CoreService.MealService.IsMealCategoryNameInUse(newMealCategory.Name);
            if (mealCategory != null && !newMealCategory.Id.Equals(mealCategory.Id))
            {
                ViewBag.SameNameMealCategory = mealCategory;
                return View(newMealCategory);
            }
            mealCategory = Convert(newMealCategory);
            mealCategory = CoreService.MealService.UpdateMealCategoryInformation(mealCategory);
            return RedirectToAction("MealCategoryDetail", new { id = newMealCategory.Id, headerMessage = "Update Success. " });
        }
        private MealCategory Convert(MealCategoryModel newMealCategory)
        {
            var mealCategory = new MealCategory
            {
                Id = newMealCategory.Id,
                Name = newMealCategory.Name,
                Price = newMealCategory.Price,
                DishTemplates = newMealCategory.DishTemplates,
                Description = newMealCategory.Description,
            };
            return mealCategory;
        }
        #endregion

        #region for DishCategories in MealCategory
        public ActionResult ManyDishCategoriesPartial(string mealCategoryId = null)
        {
            var dishCategories = CoreService.DishService.AllDishCategories();
            if (mealCategoryId != null)
            {
                ViewBag.SelectedDishTemplates = CoreService.MealService.MealCategoryDetail(mealCategoryId).DishTemplates;
            }
            List<DishCategory> dishCategoriesWithoutMealCategory = new List<DishCategory>();
            List<DishCategory> dishCategoriesWithMealCategory = new List<DishCategory>();
            foreach (var dishCategory in dishCategories)
            {
                if (dishCategory.Category == null)
                {
                    dishCategoriesWithoutMealCategory.Add(dishCategory);
                }
                else
                {
                    if (mealCategoryId != null && mealCategoryId.Equals(dishCategory.Category.Id))
                    {
                        continue;
                    }
                    else
                    {
                        dishCategoriesWithMealCategory.Add(dishCategory);
                    }
                }
            }
            ViewBag.DishCategoriesWithoutMealCategory = dishCategoriesWithoutMealCategory;
            ViewBag.DishCategoriesWithMealCategory = dishCategoriesWithMealCategory;
            return PartialView();
        }
        #endregion

        //public ActionResult DishCategoriesJSON(string id)
        //{
        //    var dishCategories = CoreService.DishService.DishCategoriesInMealCategory(id);
        //    var dishCategoriesJSON = dishCategories.Select(c => new { Id = c.Id, Name = c.Name });
        //    return Json(dishCategoriesJSON);
        //}
    }
}
